package com.hitqz.robot.dispatch.netty.dispatch;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.cache.Cache;
import com.hitqz.robot.api.business.entity.JobCachesEntity;
import com.hitqz.robot.api.business.entity.RobotEntity;
import com.hitqz.robot.biz.service.JobCachesService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 任务分配
 * @author xupkun
 * @date 2024/8/21
 */

@Component("taskDispatch")
@RequiredArgsConstructor
@Slf4j
public class TaskDispatch {

    private final JobCachesService jobCachesService;

    private static final String lock = "robot_task_lock";
    //private final JobAlgorithmFactory jobAlgorithmFactory;

//    @Scheduled(fixedRate = 3000)
//    public void checkJob() {
//        Cache<Integer, List<RobotEntity>> posInfoDtoCache = SpringUtil.getBean("robotDispatch");
//        for (Integer groupId: posInfoDtoCache.asMap().keySet()) {
//            synchronized (lock.intern()) {
//                JobCachesEntity jobCachesEntityList = jobCachesService.getOne(Wrappers.<JobCachesEntity>lambdaQuery()
//                        .eq(JobCachesEntity::getGroupId, groupId)
//                        .orderByAsc(JobCachesEntity::getSn)
//                        .last(" limit 1"));
//                if (jobCachesEntityList != null) {
//                    SpringUtil.getApplicationContext().publishEvent(jobCachesEntityList);
//                }
//            }
//        }
//    }


    //
    //@Scheduled(fixedDelay = 3000)
    //public void checkJob() {
    //
    //}


}
